package org.dei.perla.sys.console.core;

import java.net.Socket;
import java.util.HashMap;

import org.dei.perla.sys.console.messages.ConsoleMessage;
import org.dei.perla.sys.console.messages.InjectQueryMessage;
import org.dei.perla.sys.console.messages.TestQueryMessage;
import org.dei.perla.sys.console.messages.VersionMessage;
import org.dei.perla.sys.console.messages.ConsoleMessage.PerLaRequest.CommandType;
import org.dei.perla.sys.console.messages.ConsoleMessage.PerLaResponse.ResultType;
import org.dei.perla.utils.PerlaRuntime;

public class CommandUtils {

	private static HashMap<String, String> helpMap = CommandUtils.initHelpMap();
	
	public static final String GENERIC_HELP = "syntax: help <command>";

	private static final String INJECT_HELP = "inject";

	private static final String TEST_HELP = "test";

	public static final String INJECT_COMMAND_SUCCESS = "query successful injected";

	public static final String INJECT_COMMAND_FAILURE = "query parsing failed";
	
	public static String getHelp(String s){
		if(s == null)
			return GENERIC_HELP;
		if(helpMap.containsKey(s)){
			return helpMap.get(s);
		}
		return GENERIC_HELP;	
	}

	private static HashMap<String, String> initHelpMap() {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(INJECT_HELP, "syntax: inject \"<A QUERY>\"");
		map.put(TEST_HELP, "syntax: test \"<A QUERY>\"");
		return map;
	}

	public static boolean versionCommandRequest(Socket socket){
		ConsoleMessage.PerLaRequest.Builder builder = ConsoleMessage.PerLaRequest.newBuilder();
		builder.setRequestHeader(CommandType.SHOW_VERSION);
		return ConsoleUtils.sendRequest(builder.build(), socket);
	} 

	public static boolean versionCommandResponse(Socket socket){
		ConsoleMessage.PerLaResponse.Builder builder = ConsoleMessage.PerLaResponse.newBuilder();
		VersionMessage.VersionResponse.Builder queryMessageBuilder = VersionMessage.VersionResponse.newBuilder();
		builder.setResponseHeader(ResultType.VERSION_RESULT);
		queryMessageBuilder.setVersionString(PerlaRuntime.getVersion());
		builder.setVersionReponseMessage(queryMessageBuilder.build());
		
		return ConsoleUtils.sendResponse(builder.build(), socket);
	} 

	
	public static boolean injectCommandRequest(Socket socket, String query_str){
		ConsoleMessage.PerLaRequest.Builder builder = ConsoleMessage.PerLaRequest.newBuilder();
		InjectQueryMessage.InjectQueryRequest.Builder queryMessageBuilder = InjectQueryMessage.InjectQueryRequest.newBuilder();
		builder.setRequestHeader(CommandType.INJECT_QUERY);
		queryMessageBuilder.setQueryToInject(query_str);
		builder.setInjectQueryRequestMessage(queryMessageBuilder.build());
		
		return ConsoleUtils.sendRequest(builder.build(), socket);
	} 

	public static boolean injectCommandResponse(Socket socket, String query_str){
		ConsoleMessage.PerLaResponse.Builder builder = ConsoleMessage.PerLaResponse.newBuilder();
		InjectQueryMessage.InjectQueryResponse.Builder queryMessageBuilder = InjectQueryMessage.InjectQueryResponse.newBuilder();
		builder.setResponseHeader(ResultType.INJECT_RESULT);
		queryMessageBuilder.setQueryError(query_str);
		builder.setInjectQueryResponseMessage(queryMessageBuilder.build());
		
		return ConsoleUtils.sendResponse(builder.build(), socket);
	} 
	
	public static boolean testCommandRequest(Socket socket, String query_str){
		ConsoleMessage.PerLaRequest.Builder builder = ConsoleMessage.PerLaRequest.newBuilder();
		TestQueryMessage.TestQueryRequest.Builder queryMessageBuilder = TestQueryMessage.TestQueryRequest.newBuilder();
		builder.setRequestHeader(CommandType.TEST_QUERY);
		queryMessageBuilder.setQueryToTest(query_str);
		builder.setTestQueryRequestMessage(queryMessageBuilder.build());
		
		return ConsoleUtils.sendRequest(builder.build(), socket);
	}

	public static boolean testCommandResponse(Socket socket, String query_str){
		ConsoleMessage.PerLaResponse.Builder builder = ConsoleMessage.PerLaResponse.newBuilder();
		TestQueryMessage.TestQueryResponse.Builder queryMessageBuilder = TestQueryMessage.TestQueryResponse.newBuilder();
		builder.setResponseHeader(ResultType.TEST_RESULT);
		queryMessageBuilder.setQueryError(query_str);
		builder.setTestQueryResponseMessage(queryMessageBuilder.build());
		
		return ConsoleUtils.sendResponse(builder.build(), socket);
	} 
	
}
